package cn.com.infohold.smartcity.activity.Approve;

import android.content.res.Resources;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import cn.com.infohold.smartcity.R;
import cn.com.infohold.smartcity.adapter.Approve_Adapter;
import cn.com.infohold.smartcity.adapter.LandInfo_Adapter;
import cn.com.infohold.smartcity.fragment.approve_manage.Frgm_Approve;
import cn.com.infohold.smartcity.fragment.approve_manage.Frgm_Not_Approve;
import cn.com.infohold.smartcity.parent.ParentActivity;
import cn.com.infohold.smartcity.utils.RecyclerViewDivider;
import common.annotation.ViewInject;
import library.utils.DisplayUtil;

/**
 * Created by Monsoon on 2017/11/7.
 */
public class Approve_Activity extends ParentActivity{

    @ViewInject(R.id.tv_title)
    TextView tv_title;
    @ViewInject(R.id.ib_back)
    ImageButton ib_back;
    @ViewInject(R.id.mTablayout)
    TabLayout mTablayout;
    @ViewInject(R.id.vp_content)
    ViewPager mViewPager;

    private List<String> mTabs;
    private List<Fragment> mFragments;
    private Frgm_Approve frgm_approve;
    private Frgm_Not_Approve frgm_not_approve;

    @Override
    protected Object inflateView() {
        return R.layout.activity_tab_pager;
    }

    @Override
    protected void initView(View anchor) {

        tv_title.setText("审批管理");

        ib_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
                overridePendingTransitionFade();
            }
        });
        initTab();
        initViewPager();
    }

    private void initTab(){
        //mTablayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        mTablayout.setTabTextColors(ContextCompat.getColor(this, R.color.black),ContextCompat.getColor(this,R.color.colorPrimary));
        mTablayout.setSelectedTabIndicatorColor(getResources().getColor(R.color.colorPrimary));
        ViewCompat.setElevation(mTablayout,6);
        mTablayout.setupWithViewPager(mViewPager);

        mTablayout.post(new Runnable() {
            @Override
            public void run() {
                setIndicator(mTablayout,50,50);
            }
        });

    }

    private void initViewPager(){
        mTabs = new ArrayList<>();
        mTabs.add("待审批");
        mTabs.add("已审批");

        frgm_not_approve = new Frgm_Not_Approve();
        frgm_approve = new Frgm_Approve();

        mFragments = new ArrayList<>();
        mFragments.add(frgm_not_approve);
        mFragments.add(frgm_approve);

        ViewPagerAdapter mAdapter = new ViewPagerAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);

    }

    class ViewPagerAdapter extends FragmentPagerAdapter {

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mTabs.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTabs.get(position);
        }
    }

    //通过反射改变下划线长度
    public void setIndicator(TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field tabStrip = null;
        try {
            tabStrip = tabLayout.getDeclaredField("mTabStrip");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        tabStrip.setAccessible(true);
        LinearLayout llTab = null;
        try {
            llTab = (LinearLayout) tabStrip.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, leftDip, Resources.getSystem().getDisplayMetrics());
        int right = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightDip, Resources.getSystem().getDisplayMetrics());

        for (int i = 0; i < llTab.getChildCount(); i++) {
            View child = llTab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }

    }

}
